<template>
	<view class="content fofa">
		<tabnav title="设置签署信息" :col="back"></tabnav>
		<!-- <view style="height: 40rpx;"></view> -->
		<view class="wid90 mar bacFFF borRad20 box1 mar-top30" style="" v-if="Object.keys(initiator).length > 0">
			<view style="height: 20rpx;"></view>
			<view class="wid90 mar ">
				<view class="dis disAl foSi25">
					<view class="dis disAl disJuC coFFF borRad10" style="width: 100rpx;height: 40rpx;"
						:style="'background-color: #bf9b36;'">发起方</view>
					<view class="mar-left20">{{initiator.enterprise_name}}</view>
				</view>
				<!-- :style="switchTr?'background-color: '+back+';':''" -->
				<view class="foSi25 mar-top20 borRad10">
					<view class="dis disAl disJuB">
						<view class="dis disAl">
							<view class="dis disAl foSi25">
								<view class="dis disAl disJuC coFFF borRad10" style="width: 100rpx;height: 40rpx;"
									:style="'background-color: '+back+';'">签署方</view>
								<view v-if="Object.keys(signedby_user).length > 0"
									@click="tiaozhuan('/pages/template/replenish-file-initate')" class="mar-left20">
									{{signedby_user.approverType == 'ORGANIZATION' ? signedby_user.organizationName+'('+signedby_user.approveName+')' : signedby_user.approveName}}
								</view>
							</view>
							<!-- <view v-if="!switchTr" class="mar-left20">待对方填写</view> -->
							<!-- <view v-if="switchTr && replenish" class="mar-left20"></view> -->
						</view>
						<!-- <switch @change="switch1Change" :color="back" style="transform:scale(0.6)"/> -->
						<view v-if="Object.keys(signedby_user).length <= 0"
							@click="tiaozhuan('/pages/template/replenish-file-initate')"
							class="hei60 dis disAl wid90 mar" :style="'color: '+back+';'">点击补充签署方信息</view>
					</view>

				</view>
			</view>
			<view style="height: 20rpx;"></view>
		</view>
		<view class="wid90 mar bacFFF borRad20 box1 mar-top30" v-else>
			<!-- 您无权限发起合同 -->
		</view>
		<view class="mar bacFFF borRad20 box1 mar-top30" style="padding: 20rpx;width: 85%;">
			<view style="display: flex;height: 70rpx;align-items: center;">
				<view style="font-size: 30rpx;color:#807e7e">合同名称</view>
				<view style="margin-left: 20rpx;font-size: 28rpx;width: 70%;"><input type="text" v-model="file_name"
						placeholder="请输入合同名称,最多50个字" /></view>
			</view>
			<view class="xian"></view>
			<view
				style="display: flex;align-items: center;justify-content: space-between;height: 70rpx;align-items: center;"  @click="see_file">
				<view class="" style="display: flex;">
					<view style="font-size: 30rpx;color:#807e7e">合同文件</view>
					<view style="margin-left: 20rpx;font-size: 28rpx;">已上传</view>

				</view>
				<view><img :src="aurl+icon_list.youkh" alt="" style="width: 15rpx;height: 30rpx" /></view>
			</view>
			<view class="xian"></view>
			<view style="display: flex;align-items: center;height: 70rpx;align-items: center;">
				<view class="" style="display: flex;">
					<view style="font-size: 30rpx;color:#807e7e">签署截止</view>
					<view style="margin-left: 20rpx;font-size: 28rpx;">
						<picker @change="bindPickerChange" :value="dead_index" :range="dead_list">
							<view class="uni-input">合同发起后{{dead_list[dead_index]}}</view>
						</picker>
					</view>

				</view>

			</view>
		</view>

		<view style="height: 220rpx;" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}"></view>
		<view class="wid100 dis disAl disJuC poFix bacFFF box1" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}" style="height: 120rpx;bottom: 0;z-index: 999;">
			<view class="dis disAl disJuC coFFF hei80 foSi35 borRad10" @click="tijiao"
				:style="'background-color: '+back+';'" style="width: 400rpx;">下一步</view>
		</view>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	import formInput from "../common/formInput/formInput.vue"
	export default {
		components: {
			tabnav,
			formInput
		},
		data() {
			return {
				paddingBottomHeight: 0,
				aurl: this.global.url,
				back: uni.getStorageSync('back'),
				backTwo: uni.getStorageSync('backTwo'),
				file_contract_id: '',
				tops: '',
				switchTr: true,
				replenish: '',
				icon_list: {
					youkh: '/addon/qianyu_dzq/wechatimg/youkh.png'
				},
				initiator: {},
				file_name: '', // 合同名称
				deadline: '', // 合同截至时间
				dead_index: 6,
				dead_list: [
					'1天', '2天', '3天', '4天', '5天', '6天', '7天', '8天', '9天', '10天', '11天', '12天', '13天', '14天', '15天',
					'16天', '17天', '18天', '19天', '20天', '21天', '22天', '23天', '24天', '25天', '26天', '27天', '28天', '29天',
					'30天'
				],
				dead_list_int: [
					1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
					28, 29, 30
				],
				initiator_user: {}, // 发起人
				signedby_user: {}, // 签署人
			}
		},
		onLoad(option) {
			var that = this
			if(option.file_contract_id){
				that.file_contract_id = option.file_contract_id
				that.file_name = option.file_name
			}else{
				that.file_contract_id = uni.getStorageSync('file_contract_id')
			}
			uni.$on('replenish', function(data) {
				console.log('监听到事件来自 updateData ，携带参数为：' + data);
				that.replenish = data
			})
			this.get_entinfo()
		},
		onShow() {
			this.initiator_user = uni.getStorageSync('initiator_user')
			this.signedby_user = uni.getStorageSync('signedby_user')
			console.log('签署人信息')
			console.log('发起人信息')
			
			console.log(this.signedby_user)
			console.log(this.initiator_user)

		},
		created() {
			var that = this
			uni.getSystemInfo({
				success: (e) => {
					console.log(e)
					that.tops = e.statusBarHeight
					let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
					model.forEach(item => {
						//适配iphoneX以上的底部，给tabbar一定高度的padding-bottom
						if(e.model.indexOf(item) != -1 && e.model.indexOf('iPhone') != -1) {
							that.paddingBottomHeight = 40;
						}
					})
				}
			})
		},
		methods: {
			// 查看合同文件
			see_file() {
				let file_url = uni.getStorageSync('file_url')
				uni.navigateTo({
					url:'/pages/index/fileview?file_url='+file_url
				})
			},
			tijiao() {
				if (this.file_name == '') {
					return uni.showToast({
						title: '请输入合同名称',
						icon: 'error'
					})
				}
				if (this.file_name.length > 50) {
					return uni.showToast({
						title: '合同名称最大50个字符',
						icon: 'error'
					})
				}
				if (Object.keys(this.initiator_user).length <= 0 || Object.keys(this.signedby_user).length <= 0) {
					return uni.showToast({
						title: '请补充签署人',
						icon: 'error'
					})
				}
				console.log('提交啊')
				// 开始处理控件信息
				let appuser = [];
				appuser.push(this.initiator_user)
				appuser.push(this.signedby_user)
				let aldata = {
					agent: this.initiator_user.organizationOpenId, // 子公司标识
					flowName: this.file_name,
					flowApproverInfo: appuser, //签署人列表
					fileIds: this.file_contract_id,
					deadline: this.getstrtotime()
				}
				
				// 先存起来做草稿
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_drafts_creat_data',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {
						content:JSON.stringify(aldata)
					},
					success(res) {
						if(res.data.code == 1){
							// 保存成功
							uni.setStorageSync('drafts_id',res.data.data)
							uni.setStorageSync('drafts_data',aldata)
							uni.navigateTo({
								url: '/pages/template/replenish-file-control'
							})
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:'error'
							})
						}
						
					},
					complete() {}
				})
				
			},
			getstrtotime() {
				return this.getDaysAfterTimestamp(this.dead_list_int[this.dead_index])
			},
			getDaysAfterTimestamp(days) {
				const date = new Date();
				date.setDate(date.getDate() + days);
				return Math.floor(date.getTime() / 1000);
			},
			bindPickerChange(e) {
				this.dead_index = e.detail.value
				console.log(e)
			},
			get_entinfo() {
				let that = this
				uni.request({
					url: that.global.url + 'api/qianyu_dzq/applet_signatory_information',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('inuse_userinfo_token')
					},
					data: {},
					success(res) {
						console.log('团队')
						console.log(res)
						that.initiator = res.data.data
					},
					complete() {}
				})
			},


			switch1Change: function(e) {
				console.log('switch1 发生 change 事件，携带值为', e.detail.value)
				this.switchTr = e.detail.value
			},
			tiaozhuan(url) {
				uni.setStorageSync('initiator', this.initiator)
				
				uni.navigateTo({
					url: url
				});
			},


			goNativeindex(path) {
				uni.openEmbeddedMiniProgram({
					appId: 'wxa023b292fd19d41d', // 此为 appid
					path: path, // 此为路径
					envVersion: "release",
					success: res => {
						// 打开成功
						console.log("打开成功", res);
					},
					fail: err => {
						console.log(err);
					}
				})
			},
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
	}

	.xia {
		height: 1rpx;
		background: #BBBBBB;
	}

	.xian {
		width: 100%;
		border-bottom: 1px solid #e6e6e6;
		margin-top: 15rpx;
		margin-bottom: 15rpx;
	}
</style>